<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 

include_once("./root.inc.php");
include_once("./mysql/database_info.php");
include_once("./mysql/common.php");
include_once("./common.php");

connect_select_test("SHCCLibrary");

$checkedOut = mysql_query("SELECT * FROM `SHCCLibrary` WHERE `copies_avail` < `orig_copies` ");
$today = strtotime("now");
$aWeekFromToday = strtotime("+1 week");
$console = null;

while ($resultArray = mysql_fetch_array($checkedOut)) {

	if (($resultArray['orig_copies'] != 1)) {
		
		$multiCheckedOut = mysql_query("SELECT * FROM `SHCCLibrary_ ". $resultArray['id']."` WHERE `copies_avail` != '1'");
		
		while ($multiArray = mysql_fetch_array($multiCheckedOut)){
			
			if (strtotime($multiArray['due']) <= $today ){
			
				mysql_query("UPDATE `SHCCLibrary` SET `overdue`='1' WHERE `id`='".$resultArray['id']."'");
				mysql_query("UPDATE `SHCCLibrary_ ".$resultArray['id']."` SET `overdue`='1' WHERE `id`='".$multiArray['id']."'");
								
				if (mail($multiArray['email'] , "Your Book Is Due!", wordwrap("The book '". $multiArray['title'] ."' that you checked out from South Hill Calvary Chapel is due now. If you have any questions or concerns please contact the church office at 253-841-5680 or webservant@southhillcalvary.org.", 70), "From: officelibrary@southhillcalvary.org")){
					
					$console .= "Emailed: \"".$multiArray['who_has']."\" about their book: \"".$multiArray['title']."\" due: \"".$multiArray['due']."\"\n\n";
				} 
				else $console .= "Email to \"".$multiArray['who_has']."\" about their book \"".$multiArray['title']."\" failed to send\n\n";
			}
			else if (strtotime($multiArray['due']) == $aWeekFromToday) {
				
				if (mail($multiArray['email'] , "One Week Notice", wordwrap("The book '". $multiArray['title'] ."' that you checked out from South Hill Calvary Chapel is due in one week. If you have any questions or concerns please contact the church office at 253-841-5680 or webservant@southhillcalvary.org.", 70), "From: officelibrary@southhillcalvary.org")){
					
					$console .= "Emailed: \"".$multiArray['email']."\" about their book: \"".$multiArray['title']."\" due: \"".$multiArray['due']."\"\n\n";
				} 
				else $console .= "Email to \"".$multiArray['who_has']."\" about their book \"".$multiArray['title']."\" failed to send\n\n";			
			} 
		}	
	} else
		if (strtotime($resultArray['due']) <= $today ) {
		
			mysql_query("UPDATE `SHCCLibrary` SET `overdue`='1' WHERE `id`='".$resultArray['id']."'");
			
			if (mail($resultArray['email'] , "Your Book Is Due!", wordwrap("The book '". $resultArray['title'] ."' that you checked out from South Hill Calvary Chapel is due now. If you have any questions or concerns please contact the church office at 253-841-5680 or webservant@southhillcalvary.org.", 70), "From: officelibrary@southhillcalvary.org")){
				
				$console .= "Emailed: \"".$resultArray['who_has']."\" about their book: \"".$resultArray['title']."\" due: \"".$resultArray['due']."\"\n\n";
			} 
			else $console .= "Email to \"".$resultArray['who_has']."\" about their book \"".$resultArray['title']."\" failed to send\n\n";
		}
		else if (strtotime($resultArray['due']) == $aWeekFromToday) {
			
			if (mail($resultArray['email'] , "One Week Notice", wordwrap("The book '". $resultArray['title'] ."' that you checked out from South Hill Calvary Chapel is due in one week. If you have any questions or concerns please contact the church office at '253-841-5680'.", 70), "From: webservant@southhillcalvary.org")){
				
				$console .= "Emailed: \"".$resultArray['who_has']."\" about their book: \"".$resultArray['title']."\" due: \"".$resultArray['due']."\"\n\n";
			
			} else $console .= "Email to \"".$resultArray['who_has']."\" about their book \"".$resultArray['title']."\" failed to send\n\n";		
		}

}

if ($console == null){
	
	$console = "Good morning,\nNo emails where sent today.";
}

printf("%s", $console);

mysql_close();
?>